package com.prs.crm.service.stock.impl;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import com.prs.crm.context.Constrains;
import com.prs.crm.dao.stock.StoreInDao;
import com.prs.crm.domain.flow.WorkFlowProcess;
import com.prs.crm.domain.purchase.PurchaseOrder;
import com.prs.crm.domain.purchase.PurchaseOrderDetail;
import com.prs.crm.domain.purchase.PurchaseStoreIn;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.domain.store.StoreIn;
import com.prs.crm.domain.store.StoreInDetail;
import com.prs.crm.domain.store.StoreRoom;
import com.prs.crm.service.impl.FlowServiceImpl;
import com.prs.crm.service.purchase.PurchaseOrderDetailService;
import com.prs.crm.service.purchase.PurchaseOrderService;
import com.prs.crm.service.purchase.PurchaseStoreInService;
import com.prs.crm.service.stock.ProductStoreService;
import com.prs.crm.service.stock.StoreInService;

public class StoreInServiceImpl extends FlowServiceImpl<StoreIn, StoreInDao>
		implements StoreInService {

	@Autowired
	private ProductStoreService productstoreService;
	private PurchaseOrderService purchaseOrderService;
	private PurchaseOrderDetailService purchaseOrderDetailService;
	private PurchaseStoreInService purchaseStoreInService;

	@Override
	public void save(StoreIn storein, Integer[] storeinNum, String[] names,
			Product[] detailid) {
		for (int i = 0; i < storeinNum.length; i++) {
			//			
			// int size =
			// checkRecord(detailid[i].getId(),storein.getRoom().getId(),storein.getBatchNum());
			// if (size == 0) {
			// saveNewProductStore(storein, detailid[i], storeinNum[i]);
			// } else {
			// updateProductStore(storeinNum[i],storein.getRoom(),
			// detailid[i],storein.getBatchNum());
			// }
			
			StoreInDetail detail = new StoreInDetail();
			detail.setName(names[i]);
			detail.setStoreinNum(storeinNum[i]);
			detail.setProduct(detailid[i]);
			storein.addDetail(detail);

		}
		super.save(storein);
	}

	@Override
	public void onProcessEnd(WorkFlowProcess process) {

		super.onProcessEnd(process);

		StoreIn project = this.getProcessEntity(process);
		Set<StoreInDetail> details = project.getDetails();
		if (process.getResult() == Constrains.WORKFLOWPROCESS_RESULT_PASS) {
			int purchaseOrderId = project.getPurchaseOrderId();
			PurchaseOrder purchaseOrder = this.getPurchaseOrderService()
					.get(purchaseOrderId);
			if (purchaseOrder != null) {
				PurchaseStoreIn in=new PurchaseStoreIn();
				in.setBatchNum(project.getBatchNum());
				//in.set(storein.getStoreInCode());
				in.setPurchaseOrder(purchaseOrder);
				in.setRooms(project.getRoom());
				in.setStoreInDate(project.getStoragedata());
				in.setStoreName(project.getTitlename());
				this.getPurchaseStoreInService().save(in);
				
			}
			for (StoreInDetail detail : details) {
				int size = checkRecord(detail.getProduct().getId(), project
						.getRoom().getId(), project.getBatchNum());
				if (size == 0) {
					saveNewProductStore(project, detail.getProduct(), detail
							.getStoreinNum());
				} else {
					updateProductStore(detail.getStoreinNum(), project
							.getRoom(), detail.getProduct(), project
							.getBatchNum());
				}
			}
		} else {
			if (project.getPurchaseOrderId() != 0) {
				int purchaseOrderId = project.getPurchaseOrderId();
				PurchaseOrder purchaseOrder = this.getPurchaseOrderService()
						.get(purchaseOrderId);
				if (purchaseOrder != null) {
					Set<PurchaseOrderDetail> details2 = purchaseOrder
							.getDetails();
					for (PurchaseOrderDetail orderDetail : details2) {

						for (StoreInDetail storeInDetail : details) {
							if (orderDetail.getProduct() == storeInDetail
									.getProduct()) {
								orderDetail.setInNum(orderDetail.getInNum()
										- storeInDetail.getStoreinNum());
								orderDetail.setInType(0);
								purchaseOrderDetailService.save(orderDetail);
							}
						}
					}

					purchaseOrder.setPurStoreState(0);
					purchaseOrderService.save(purchaseOrder);
				}

			}

		}
	}

	@Override
	public boolean valideSubmit(StoreIn entity) {

		return true;
	}

	/**
	 * 
	 * add
	 */
	public void saveNewProductStore(StoreIn storein, Product product,
			int stocknum) {
		ProductStore ps = new ProductStore();
		ps.setRoom(storein.getRoom());
		ps.setProduct(product);
		ps.setProductNum(stocknum);
		ps.setStorecode(storein.getRoom().getRoomcode());
		ps.setBatchNum(storein.getBatchNum());
		this.getProductstoreService().save(ps);
	}

	/**
	 * 
	 *update
	 */
	public void updateProductStore(int stocknum, StoreRoom room,
			Product product, String batchNum) {
		ProductStore prst = this.getProductstoreService().getStore(room,
				product, batchNum);
		prst.setProductNum(prst.getProductNum() + stocknum);
		this.getProductstoreService().savePrStore(prst);

	}

	public int checkRecord(int product, int room, String batchNum) {
		List<ProductStore> slist = this.getProductstoreService()
				.getProductStoreSize(product, room, batchNum);

		return slist.size();
	}

	public ProductStoreService getProductstoreService() {
		return productstoreService;
	}

	public void setProductstoreService(ProductStoreService productstoreService) {
		this.productstoreService = productstoreService;
	}

	public PurchaseOrderService getPurchaseOrderService() {
		return purchaseOrderService;
	}

	public void setPurchaseOrderService(
			PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}

	public PurchaseOrderDetailService getPurchaseOrderDetailService() {
		return purchaseOrderDetailService;
	}

	public void setPurchaseOrderDetailService(
			PurchaseOrderDetailService purchaseOrderDetailService) {
		this.purchaseOrderDetailService = purchaseOrderDetailService;
	}

	public PurchaseStoreInService getPurchaseStoreInService() {
		return purchaseStoreInService;
	}

	public void setPurchaseStoreInService(
			PurchaseStoreInService purchaseStoreInService) {
		this.purchaseStoreInService = purchaseStoreInService;
	}


}
